package stream.exercise;

import lambda.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;


public class TestStreamlianxi1 {

    /**
     * 1. 给定一个数字列表，如何返回一个由每个数的平方构成的列表呢？
     * 给定 [1,2,3,4,5] 返回 [1,4,9,16,25]
     */

    @Test
    public void test1() {
        List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5);
        // 1. 使用map(映射) 获取
        intList.stream()
                .map(x -> x * x)
                .forEach(System.out::println);
    }

    /**
     * 2. 怎么样使用 map 和 reduce 数一数流中有多少个 Employee 呢
     */

    List<Employee> employees = Arrays.asList(
            new Employee("张三",18,9999.99),
            new Employee("李四",58,5555.55),
            new Employee("王五",26,3333.33),
            new Employee("赵六",36,6666.66),
            new Employee("田七",12,8888.88),
            new Employee("田七",12,8888.88),
            new Employee("田七",12,8888.88)
    );

    @Test
    public void test2() {
        Optional<Integer> count = employees.stream()
                .map(e -> 1)
                .reduce(Integer::sum);
        System.out.println(count.get());
    }
}
